package com.ssy.dao;

import com.ssy.common.PageInfo;
import com.ssy.entity.User;
import com.ssy.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Map;

/**
 * UserDao
 *
 * @author ssy
 * @date 2020/11/12
 **/
public class UserDao extends BaseDao {

    public User queryByUserNameAndPwd(String userName, String passWord) {
        String sql = "SELECT u_id AS uId,u_name AS uname,u_pwd AS upwd,u_type AS utype,createTime as createTime,updateTime as updateTime FROM user WHERE u_name=? AND u_pwd=? ";
        return this.selectOne(sql, User.class, userName, passWord);
    }

    public PageInfo<User> selectPage(Map<String, Object> params, Integer pageNo, Integer limitNo) {
        String sql = "select u_id as uId,u_name as uname,u_pwd as upwd,u_type as utype,createTime as createTime ,updateTime as updateTime from user where";
        if (params.containsKey("uname")) {
            sql = sql + " u_name like '%" + params.get("uname") + "%'  and";
        }
        if (params.containsKey("utype")) {
            sql = sql + " u_type like '%" + params.get("utype") + "%'  and";
        }
        if (sql.endsWith("and")) {
            sql = sql.substring(0, sql.lastIndexOf("and"));
        }
        if (sql.endsWith("where")) {
            sql = sql.substring(0, sql.lastIndexOf("where"));
        }
        return this.selectPage(sql, User.class, pageNo, limitNo);
    }

    public boolean insertUser(String uname, int utype, String s, String createTime, String updateTime) {
        String sql = "insert into user(u_name,u_pwd,u_type,createTime,updateTime) value(?,?,?,?,?)";
        int userid = this.insert(sql, uname, s, utype, createTime, updateTime);
        return userid > 0 ? true : false;
    }

    public boolean batchDeleteUsers(String[] ids) {
        String sql = "delete from user where u_id=?";
        return this.batchDelete(sql, ids);
    }

    public boolean deleteUser(String id) {
        String sql = "delete from user where u_id=?";
        return this.update(sql, id);
    }

    public boolean restUserPwd(String id, String md5Pwd) {
        String sql = "update user set u_pwd=? where u_id=?";
        return this.update(sql, md5Pwd, id);
    }

    public boolean updateUsers(String uname, int utype, String updateTime, int uId) {
        String sql = "UPDATE USER SET u_name=?,u_type=?,updateTime=? WHERE u_id=?";
        return this.update(sql, uname, utype, updateTime, uId);
    }

    public User selectOneUser(String uname) {
        String sql = "SELECT u_id AS uId FROM USER WHERE u_name=?";
        return this.selectOne(sql, User.class, uname);
    }

    public User selectUserByUid(int uId) {
        String sql = "SELECT u_name AS uname,u_pwd AS upwd  FROM USER WHERE u_id=?";
        return this.selectOne(sql, User.class, uId);
    }

    public boolean updateUserPwd(String newPassword, int uId) {
        String sql = "UPDATE user SET u_pwd=? WHERE u_id=?";
        return this.update(sql, newPassword, uId);

    }
}
